package com.yociyy.auth.biz;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.yociyy.admin.feign.RemoteUserService;
import com.yociyy.admin.model.constant.UserConstants;
import com.yociyy.admin.model.entity.BaseUser;
import com.yociyy.common.constants.Constants;
import com.yociyy.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户鉴权
 * 
 * @author: YoCiyy
 * @date: 2020/4/15
 */
@Service
public class AuthBiz {

	@Autowired
	private RemoteUserService remoteUserService;

	private BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(UserConstants.PW_ENCORDER_SALT);

	private final static long EXPIRE = 12 * 60 * 60;

	private final static String ACCESS_TOKEN = Constants.ACCESS_TOKEN;

	private final static String ACCESS_USERID = Constants.ACCESS_USERID;

	@Autowired
	private RedisUtils redis;

	@Autowired
	private AuthenticationManager authenticationManager;

	/**
	 * 校验用户登录
	 * 
	 * @param account 登录账号
	 * @param password 密码
	 * @return 用戶信息
	 */
	public BaseUser login(String account, String password) {

		// 查询用户信息
		BaseUser user = remoteUserService.selectUserByAccount(account);
//
//		if (ObjectUtil.isEmpty(user)) {
//			throw new UserNotExistsException();
//		}

		// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
		Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(account, password));
//		LoginUser loginUser = (LoginUser) authentication.getPrincipal();

//		if (!encoder.matches(password, user.getPassword())) {
//			throw new UserPasswordNotMatchException();
//		}
		return user;
	}

	/**
	 * 创建token
	 * 
	 * @param user 用户信息
	 * @return 结果
	 */
	public Map<String, Object> createAccessToken(BaseUser user) {
		// 生成token
		String token = IdUtil.randomUUID();
		// 保存、更新用户token
		Map<String, Object> map = new HashMap<String, Object>(3);
		map.put("userId", user.getId());
		map.put("token", token);
		map.put("expire", EXPIRE);

		// 存储用户信息至redis
		redis.set(ACCESS_TOKEN + token, user, EXPIRE);
		// 存储token至redis
		redis.set(ACCESS_USERID + user.getId(), token, EXPIRE);
		return map;
	}

	/**
	 * 通过token查询用户信息
	 * 
	 * @param token token信息
	 * @return 用户信息
	 */
	public BaseUser queryByToken(String token) {
		return redis.get(ACCESS_TOKEN + token, BaseUser.class);
	}

	/**
	 * 用户注销登录、移除token信息
	 * 
	 * @param id 用户ID
	 */
	public void expireToken(Long id) {
		String token = redis.get(ACCESS_USERID + id);
		if (StrUtil.isNotEmpty(token)) {
			// 移除用户信息
			redis.delete(ACCESS_USERID + id);
			// 移除token
			redis.delete(ACCESS_TOKEN + token);
		}
	}
}
